package com.yjc.drygoods.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjc.common.to.SpuCommentCountTo;
import com.yjc.common.utils.PageUtils;
import com.yjc.drygoods.product.vo.CommentStarVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.product.entity.SpuCommentEntity;
import com.yjc.drygoods.product.service.SpuCommentService;
import com.yjc.common.utils.R;


/**
 * 商品评价
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 21:27:48
 */
@Slf4j
@RestController
@RequestMapping("product/spucomment")
public class SpuCommentController {
    @Autowired
    private SpuCommentService spuCommentService;

    @GetMapping("/get/all/spuComments/avgStar")
    public R getAllSpuCommentsAvgStar() {
        List<SpuCommentEntity> resList = spuCommentService.list();
        int starSum = resList.stream().mapToInt(SpuCommentEntity::getStar).reduce(0, Integer::sum);
        return R.ok().setData(starSum / resList.size());
    }

    @GetMapping("/get/sku/commentStatus/{key}")
    public R getSkuCommentStatus(@PathVariable(value = "key") String key) {
        CommentStarVo commentStarVo = spuCommentService.getSkuCommentStatus(key);
        return R.ok().setData(commentStarVo);
    }

    @PostMapping("/getBatch/skuComments")
    public R getBatchSkuComments(@RequestBody List<Long> skuIds) {
        List<SpuCommentCountTo> spuComments = spuCommentService.getBatchSkuComments(skuIds);
        return R.ok().setData(spuComments);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuCommentService.queryPage(params);
        log.info("查询到了评论信息");
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SpuCommentEntity spuComment = spuCommentService.getById(id);

        return R.ok().put("spuComment", spuComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuCommentEntity spuComment) {
        spuCommentService.save(spuComment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuCommentEntity spuComment) {
        spuCommentService.updateById(spuComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        spuCommentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
